package com.dayuanit.dy14.enter.dy14enter1024.service.impl;

import com.dayuanit.dy14.enter.dy14enter1024.entity.User;
import com.dayuanit.dy14.enter.dy14enter1024.exception.BizException;
import com.dayuanit.dy14.enter.dy14enter1024.mapper.UserMapper;
import com.dayuanit.dy14.enter.dy14enter1024.service.UserService;
import com.dayuanit.dy14.enter.dy14enter1024.service.base.BaseService;
import com.dayuanit.dy14.enter.dy14enter1024.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserServiceImpl extends BaseService implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 报名
     * @param username 用户名
     * @param email 邮箱
     * @param sex 性别
     */
    @Override
    public void signUp(String username, String email, String sex) {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(email) || StringUtils.isBlank(sex)) {
            throw new BizException("有必填参数为空");
        }

        //TODO username长度，<> /}] 以及敏感词汇。

        //TODO email校验，格式校验。

        //TODO 有木有过期
        if (new Date().after(DateUtils.string2Date(deadlineTime))) {
            throw new BizException("报名已截止");
        }

        User user = new User();
        user.setEmail(email);
        user.setSex(sex);
        user.setUsername(username);

        int rows = userMapper.insert(user);
        if (1 != rows) {
            throw new BizException("添加失败");
        }
    }
}
